package com.tinxing.bp.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author YJH
 * @date 2019/12/15 14:03
 */
@Component
public class MetaHandler implements MetaObjectHandler {
    private static final Logger logger = LoggerFactory.getLogger(MetaHandler.class);



    private static final String SHIRO_USERS = "SHIROUSERS";


    /**
     * 插入时填充字段，实体对象含注解 @TableField(fill = FieldFill.INSERT 或 FieldFill.INSERT_UPDATE)
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        //注意 这里配置两个逻辑删除是因为 sqlserver 和 mysql 不同 mysql 没有布尔类型 因此使用布尔类型会失效
        this.strictInsertFill(metaObject, "isDelete", Integer.class, 0);


    }

    /**
     * 更新时填充字段，实体对象含注解 @TableField(fill = FieldFill.UPDATE 或 FieldFill.INSERT_UPDATE)
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
    }
}
